package cn._51doit.flink.day04;

import org.apache.flink.streaming.api.datastream.AllWindowedStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;

/**
 * 按照数据的条数划分窗口
 *
 * 没有keyBy的窗口： non keyed window
 * keyBy的窗口：keyed window
 *
 */
public class CountWindowAllDemo {

    public static void main(String[] args) throws Exception {

        //没有keyBy的窗口： non keyed window
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //1
        //2
        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);

        SingleOutputStreamOperator<Integer> nums = lines.map(e -> Integer.parseInt(e));

        //不keyBy，然后按照条数划分窗口
        AllWindowedStream<Integer, GlobalWindow> window = nums.countWindowAll(5);

        SingleOutputStreamOperator<Integer> res = window.sum(0);

        res.print();

        env.execute();
    }
}
